// Uncle MiF
#ifndef _m_dict_h_
#define _m_dict_h_
////////////////////////////////////
// Some code prefs ///////////

//#define STUPID
#define PERMANENTLOOKUP
//#define VERBOSE

/////////////////////

extern const char * dictSfx;
extern const char * idxSfx;

#ifndef STUPID

typedef struct tagTArr 
{
	unsigned count;
	unsigned size;
	unsigned * items;
} TArr, *PArr;

typedef struct tagTIndexEntry
{
	char * dict;
	PArr idxArr;
} TIndexEntry, *PIndexEntry;

////////////////////////////////////
// array support
PArr newArr();// create new array
PArr freeArr(PArr arr);// free array
int reallocArrToCount(PArr arr,unsigned count);// fast realloc
void pushArrItem(PArr arr, unsigned val);// push item to array back
void freeArrItem(PArr arr,unsigned idx);// free item and moves down others
unsigned getArrItem(PArr arr, unsigned idx, unsigned * isError);// get array item
unsigned getArrCount(PArr arr);// get array items count

////////////////////////////////////
// dict index specific
PIndexEntry newIndexEntry(const char* dict);// create new index entry
PIndexEntry freeIndexEntry(PIndexEntry ie);// free index entry
PIndexEntry getIndexEntry(PArr indexes,const char* dict);// return index entry slot for dict or create one
void freeUsedIndexes();// common clean up
#define packIndexEntry(ie) ((unsigned)(ie))
#define unpackIndexEntry(pie) ((PIndexEntry)(pie))

////////////////////////////////////
// dict support

// private: ================ (don't use any private functions) ===

unsigned sumCRC(unsigned * items,unsigned count);// simple checker for index cache integrity
char* getWordFromIndex(PArr idxArr,unsigned wIdx);
char* getWordDescFromDictForLastIndex();
unsigned searchWordIndex(PArr idxArr,unsigned begin,unsigned end,unsigned * found);
char* quickWordSearch(PArr idxArr,FILE* fIdx,FILE* fDict,unsigned lenFIdx,unsigned lenFDict,const char* word);
void closeSuggestionsSession();
unsigned getSuggestionBegin(const char* dict,const char* wBegin);
unsigned getNextSuggestion(PArr idxArr,const char* wBegin,unsigned sID);
int suggestionAlreadyExists(char ** suggestions,char * suggestion);
int loadIndexCacheIndirect(PIndexEntry ie,FILE * fIdxCache);
void saveIndexCacheIndirect(PIndexEntry ie,FILE * fIdxCache);

#endif

// public: ================

// translate word using dict, dict name without any extension, free required
char* getWord(const char* dict, const char* word,int suggestionsMode);

// char* list[], free required for all items and list by it self, returns NULL on no suggestions
// last list item is NULL
char ** getSuggestions(const char* wBegin);

// char* list[], free required for all items and list by it self, returns NULL on no indexed dictionaries
// last list item is NULL
char ** getIndexedDicts();

void freePCharList(char ** list);// used for free list received from getSuggestions and getIndexedDicts

void freeUnusedDict(const char* dict);

#endif
